const express = require('express')
const path = require('path')
const app = express()

var bodyParser = require('body-parser');
app.use(bodyParser.json({
    limit: '10mb'
})); //body-parser 解析json格式数据
app.use(bodyParser.urlencoded({ //此项必须在 bodyParser.json 下面,为参数编码
    extended: true
}));

var qiniuConfig = require('./public/qiniuConfig.json');
const liveHelper = require('./liveHelper');

app.use(express.static(path.join(__dirname, 'public')))

var serverSecret = "888";

var myLogger = function (req, res, next) {
    var secret = req.query.secret || "";

    if (secret != serverSecret) {
        var result = {
            success: false,
            msg: "权限密码错误"
        };

        res.writeHead(200, {
            'Content-Type': 'application/json'
        });
        res.end(JSON.stringify(result));
        return false;
    }
    next()
}

app.use(myLogger)

app.get('/updateLiveConfig/videoKey/:videoKey', (req, res) => {

    var result = {
        success: true,
        msg: '操作成功'
    };

    console.log('进入:/updateLiveConfig/videoKey');
    //也可以更新为空  为空是说明当前无默认直播
    videoKey = req.params.videoKey || "";

    if (videoKey == -99) {
        videoKey = "";
    }


    qiniuConfig.mobile_web.videoKey = videoKey;
    qiniuConfig.pc_web.videoKey = videoKey;

    liveHelper.updteLiveConfig(qiniuConfig);

    res.writeHead(200, {
        'Content-Type': 'application/json'
    });
    res.end(JSON.stringify(result));

})

//更新所有 
app.post('/updateLiveConfig/all', (req, res) => {

    var result = {
        success: true,
        msg: '操作成功'
    };

    var qiniuConfig = req.body;

    liveHelper.updteLiveConfig(qiniuConfig);

    res.writeHead(200, {
        'Content-Type': 'application/json'
    });
    res.end(JSON.stringify(result));

})




app.listen(10003, () => console.log('Example app listening on port 10003!'))